package com.hyts.threads.factory;

import com.hyts.threads.conf.ThreadPoolConfig;
import com.hyts.threads.service.ThreadPoolService;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;


/**
 * @author libo
 * @description 线程池工厂类
 *
 */
public class ThreadPoolFactory {

		
	
	private static ThreadPoolExecutor executor;
	
		/**
		 *  开启任务集合数据操作
		 * @param service 业务逻辑实现类
		 * @param isLimited 是否进行限制缓冲大小
		 * @return
		 */
		public static <T>ThreadPoolExecutor createTaskPool(final ThreadPoolService<T> service, final T param, boolean isLimited){
				
				if(service == null){
					throw new NullPointerException("传入的ThreadPoolService 类 的类实现类为空!");
				}
				if(executor == null)
				 executor = new ThreadPoolExecutor(ThreadPoolConfig.DEFAULT_CORE_THREAD_POOL_SIZE,
																												ThreadPoolConfig.DEFAULT_MAX_THREAD_POOL_SIZE,
																												ThreadPoolConfig.DEFAULT_KEEP_ALIVE_TIME,
																												ThreadPoolConfig.DEFAULT_TIME_UNIT,
																												isLimited?new LinkedBlockingQueue():new ArrayBlockingQueue(ThreadPoolConfig.DEFAULT_BLOCKQUEUE_SIZE)){
					@Override
					public void execute(Runnable command) {
						service.execute(param);
					}
					@Override
					protected void beforeExecute(Thread t, Runnable r) {
						service.before(param);
					}
					@Override
					 protected void afterExecute(Runnable r, Throwable t) {
						 service.after(param);
					 }
					@Override
					protected void terminated() {
						service.end(param);
					}
				};
					executor.execute(new Runnable(){

					public void run() {
						System.out.println("默认匿名");
					}
					
				});
				return executor;
		}
		
		/**
		 *  开启任务集合数据操作
		 * @param service 业务逻辑实现类
		 * @param isLimited 是否进行限制缓冲大小
		 * @return
		 */
		public static <T>ThreadPoolExecutor createTaskPool(final ThreadPoolService<T> service,final boolean isLimited){
			
			if(service == null){
				throw new NullPointerException("传入的ThreadPoolService 类 的类实现类为空!");
			}
			if(executor == null)
			 executor = new ThreadPoolExecutor(ThreadPoolConfig.DEFAULT_CORE_THREAD_POOL_SIZE,
																											ThreadPoolConfig.DEFAULT_MAX_THREAD_POOL_SIZE,
																											ThreadPoolConfig.DEFAULT_KEEP_ALIVE_TIME,
																											ThreadPoolConfig.DEFAULT_TIME_UNIT,
																											isLimited?new LinkedBlockingQueue():new ArrayBlockingQueue(ThreadPoolConfig.DEFAULT_BLOCKQUEUE_SIZE)){
				@Override
				public void execute(Runnable command) {
					service.execute();
				}
				@Override
				protected void beforeExecute(Thread t, Runnable r) {
					service.before();
				}
				@Override
				 protected void afterExecute(Runnable r, Throwable t) {
					 service.after();
				 }
				@Override
				protected void terminated() {
					service.end();
				}
			};
			executor.execute(new Runnable(){

				public void run() {
					System.out.println("默认匿名");
				}
				
			});
			return executor;
	}
		
		/**
		 *  开启任务集合数据操作
		 * @param service 业务逻辑实现类
		 * @return
		 */
		public static <T>ThreadPoolExecutor createTaskPool(final ThreadPoolService<T> service){
			if(service == null){
				throw new NullPointerException("传入的ThreadPoolService 类 的类实现类为空!");
			}
			if(executor == null)
				executor = new ThreadPoolExecutor(ThreadPoolConfig.DEFAULT_CORE_THREAD_POOL_SIZE,
																											ThreadPoolConfig.DEFAULT_MAX_THREAD_POOL_SIZE,
																											ThreadPoolConfig.DEFAULT_KEEP_ALIVE_TIME,
																											ThreadPoolConfig.DEFAULT_TIME_UNIT,
																											new ArrayBlockingQueue(ThreadPoolConfig.DEFAULT_BLOCKQUEUE_SIZE)){
				@Override
				public void execute(Runnable command) {
					service.execute();
				}
				@Override
				protected void beforeExecute(Thread t, Runnable r) {
					service.before();
					
				}
				@Override
				 protected void afterExecute(Runnable r, Throwable t) {
					 service.after();
				 }
				@Override
				protected void terminated() {
					service.end();
				}
			};
			executor.execute(new Runnable(){

				public void run() {
					System.out.println("默认匿名");
				}
				
			});
			return executor;
	}	
	
		/**
		 * @Title: BuildTaskPool
		 * @Description: 开启任务集合数据操作
		 * @param runnable
		 * @return
		 * @return: ThreadPoolExecutor
		 */
		public static <T>ThreadPoolExecutor BuildTaskPool(){
			if(executor == null)
				executor = new ThreadPoolExecutor(ThreadPoolConfig.DEFAULT_CORE_THREAD_POOL_SIZE,
																											ThreadPoolConfig.DEFAULT_MAX_THREAD_POOL_SIZE,
																											ThreadPoolConfig.DEFAULT_KEEP_ALIVE_TIME,
																											ThreadPoolConfig.DEFAULT_TIME_UNIT,
																											new ArrayBlockingQueue(ThreadPoolConfig.DEFAULT_BLOCKQUEUE_SIZE));
			return executor;
	}	
		
}
